package com.jiasongfan.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.jiasongfan.entity.*;
import com.jiasongfan.service.OrderService;
import com.jiasongfan.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @RequestMapping("getTree")
    @ResponseBody
    public List<Menu> getTree(HttpSession session){
        TbUser user = (TbUser) session.getAttribute("user");
        return  orderService.getTree(user);
    }

    @RequestMapping("index")
    public String index(){

        return "index";
    }
    @RequestMapping("pic")
    public String pic(){

        return "pic";
    }
    @RequestMapping("toregister")
    public String toregister(Model model){

        return "register";
    }
    @RequestMapping("register")
    public String register(TbUser tbUser){
        String userPwd = tbUser.getUserPwd();
        String s = MD5Utils.md5Encode(userPwd);
        tbUser.setUserPwd(s);
        orderService.register(tbUser);
        return "redirect:tologin";
    }


    @RequestMapping("tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("login")
    public String login(TbUser tbUser, HttpSession session,Model model){
        //md5登录
        String pwd = tbUser.getUserPwd();
        String s = MD5Utils.md5Encode(pwd);
        tbUser.setUserPwd(s);
        TbUser user=  orderService.login(tbUser);
      if (null!=user){
          session.setAttribute("user",user);

          //用户为管理员时才能看所有信息
          return "redirect:index";
      }
        model.addAttribute("err","账号或密码错误");
        return "login";
    }

    @RequestMapping("list")
    public String list(Model model,QueryVO vo, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "2") int pageSize){
        PageInfo<TbOrder> pg=orderService.list(vo,pageNum,pageSize);
        model.addAttribute("pg",pg);
        model.addAttribute("vo",vo);
        return "list";
    }

    @RequestMapping("toadd")
    public String toadd(Model model){
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        long oid = snowflake.nextId();
        model.addAttribute("oid",oid);
        return "add";
    }
    @RequestMapping("add")
    public String add(TbOrder tbOrder){
        orderService.add(tbOrder);
        return "redirect:list";
    }

    @RequestMapping("selectById")
    public String selectById(Long oid,Model model){
       TbOrder tbOrder=orderService. selectById(oid);
       model.addAttribute("tbOrder",tbOrder);
        return "update";
    }

    @RequestMapping("update")
    public String update(TbOrder tbOrder){
        orderService. update(tbOrder);
        return "redirect:list";
    }


    //treeview
    @RequestMapping("tree")
    public String tree(){
        return "tree";
    }
}
